﻿// E-Book Copier
// Copyright (C) 2009 Chuan-Gen Fan
// 
// This program is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.
// 
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.
// 
// You should have received a copy of the GNU General Public License
// along with this program.  If not, see <http://www.gnu.org/licenses/>.
// 
// Project: http://code.google.com/p/ebookcopier/
// Contact Author:
//    website: http://chuangen.name/
// e-mail/msn: chuangen@live.cn

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Text;
using System.Windows.Forms;
using System.IO;
using System.Reflection;

namespace EbookCopier
{
    /// <summary>
    /// 连续捕捉设置窗体。
    /// </summary>
    public partial class SettingsForm : Form
    {
        /// <summary>
        /// 常量类。
        /// </summary>
        private class Consts
        {
            internal const string CompanyLink = "http://chuangen.name/";
        }
        Properties.Settings settings = Properties.Settings.Default;
        CaptureHelper captureHelper = null;
        PdfCreator pdfCreator = null;
        /// <summary>
        /// 初始化类 <see cref="SettingsForm"/> 的新实例。
        /// </summary>
        /// <param name="captureHelper">CaptureHelper 实例</param>
        /// <param name="pdfCreator">PdfCreator 实例</param>
        public SettingsForm(CaptureHelper captureHelper, PdfCreator pdfCreator)
        {
            InitializeComponent();
            this.TopMost = true;

            this.pdfCreator = pdfCreator;

            captureHelper.ReaderStateChanged += delegate(object sender, EventArgs e) { UpdateButtons(); };

            this.captureHelper = captureHelper;
            captureHelper.CaptureStateChanged += new EventHandler(captureHelper_CaptureStateChanged);

            linkCompany.LinkClicked += new LinkLabelLinkClickedEventHandler(linkCompany_LinkClicked);

            tsbSetRect.Click += new EventHandler(btnSetRect_Click);

            tsbPrevPage.Click += delegate(object sender, EventArgs e)
            {
                this.Visible = false;
                captureHelper.PrevPage();
                this.Visible = true;
            };
            tsbNextPage.Click += delegate(object sender, EventArgs e)
            {
                this.Visible = false;
                captureHelper.NextPage();
                this.Visible = true;
            };
            tsbCapture.Click += delegate(object sender, EventArgs e)
            {
                this.Visible = false;
                captureHelper.CaptureScreen();
                this.Visible = true;
            };
            btnCmderSet.Click += delegate(object sender, EventArgs e)
            {
                captureHelper.EditOptions(this);
            };

            btnOK.Click += new EventHandler(btnOK_Click);
        }

        void cmbCmderType_SelectedIndexChanged(object sender, EventArgs e)
        {
            if(cmbCmderType.SelectedIndex < 0 || cmbCmderType.SelectedIndex >= cmbCmderType.Items.Count)
                return;

            string cmderName = cmbCmderType.SelectedItem as string;
            if (cmderName == null || cmderName == "")
                return;

            captureHelper.CmderName = cmderName;

            CmderAttribute attr = CmderManager.GetCmderAttributeByName(cmderName);
            if (attr != null)
                this.btnCmderSet.Visible = attr.HasEditableOptions;
        }

        void btnOK_Click(object sender, EventArgs e)
        {
            captureHelper.CurrentIndex = (int)numFirstPageNumber.Value;
            captureHelper.LastPageIndex = (int)numLastPageNumber.Value;

            this.DialogResult = DialogResult.OK;
        }

        void captureHelper_CaptureStateChanged(object sender, EventArgs e)
        {
            UpdateButtons();
        }

        void btnSetRect_Click(object sender, EventArgs e)
        {
            this.WindowState = FormWindowState.Minimized;
            captureHelper.SetSelectedRect();
            this.WindowState = FormWindowState.Normal;
        }

        /// <summary>
        /// 引发 <see cref="E:System.Windows.Forms.Form.Load"></see> 事件。
        /// </summary>
        /// <param name="e">包含事件数据的 <see cref="T:System.EventArgs"></see>。</param>
        protected override void OnLoad(EventArgs e)
        {
            base.OnLoad(e);

            numFirstPageNumber.Value = captureHelper.CurrentIndex;
            numLastPageNumber.Value = captureHelper.LastPageIndex;

            UpdateButtons();

            cmbCmderType.Items.AddRange(CmderManager.GetCmderNames());
            cmbCmderType.SelectedItem = captureHelper.CmderName;
            if (cmbCmderType.Items.Count > 0 && cmbCmderType.SelectedIndex<0)//保证有选中项
                cmbCmderType.SelectedIndex = 0;
            cmbCmderType.SelectedIndexChanged += new EventHandler(cmbCmderType_SelectedIndexChanged);
            cmbCmderType_SelectedIndexChanged(this, EventArgs.Empty);
        }

        private void linkCompany_LinkClicked(object sender, LinkLabelLinkClickedEventArgs e)
        {//访问网站
            try
            {
                System.Diagnostics.Process.Start(Consts.CompanyLink);
            }
            catch
            {
                //无法打开网站。
            }
        }
        /// <summary>
        /// 更新各个按钮的状态。
        /// </summary>
        void UpdateButtons()
        {
            switch (captureHelper.CaptureState)
            {
                case CaptureState.Stop://停止状态，
                    //可以SetRect和Capture
                    tsbSetRect.Enabled = true;
                    tsbCapture.Enabled = true;
                    //翻页可用
                    tsbPrevPage.Enabled = true;
                    tsbNextPage.Enabled = true;
                    //开始可用，暂停和停止不可用
                    //tsbStart.Enabled = true;
                    //tsbPause.Enabled = false;
                    //tsbStop.Enabled = false;

                    ////tsbStart.Checked = false;
                    ////tsbPause.Checked = false;

                    if (captureHelper.ReaderState != ReaderState.RunningAndOpenDocument)
                    {
                        //翻页可用
                        tsbPrevPage.Enabled = false;
                        tsbNextPage.Enabled = false;

                        //tsbStart.Enabled = false;
                    }
                    break;
                case CaptureState.Pause://暂停状态
                    //可以SetRect和Capture
                    tsbSetRect.Enabled = true;
                    tsbCapture.Enabled = true;
                    //翻页可用
                    tsbPrevPage.Enabled = true;
                    tsbNextPage.Enabled = true;
                    //开始不可用，暂停被选中，停止可用
                    //tsbStart.Enabled = true;
                    //tsbPause.Enabled = true;
                    //tsbStop.Enabled = true;

                    ////tsbStart.Checked = false;
                    //tsbPause.Enabled = false;//tsbPause.Checked = true;
                    break;
                case CaptureState.Run://自动捕捉状态
                    //不可以SetRect和Capture
                    tsbSetRect.Enabled = false;
                    tsbCapture.Enabled = false;
                    //翻页不可用
                    tsbPrevPage.Enabled = false;
                    tsbNextPage.Enabled = false;
                    //开始不可用，暂停和停止可用
                    //tsbStart.Enabled = true;
                    //tsbPause.Enabled = true;
                    //tsbStop.Enabled = true;

                    //tsbStart.Enabled = false;//tsbStart.Checked = true;
                    ////tsbPause.Checked = false;
                    break;
            }
        }

    }
}